package com.runrunfast.face.service;

import com.alibaba.fastjson.JSONObject;
import com.runrunfast.face.constant.YoutuUrl;
import com.runrunfast.face.dao.CameraRepository;
import com.runrunfast.face.dao.DeviceRelationRepository;
import com.runrunfast.face.entity.Camera;
import com.runrunfast.face.entity.DeviceRelation;
import com.runrunfast.face.entity.YouBox;
import com.runrunfast.face.service.common.ColourService;
import com.runrunfast.face.service.common.YoutuService;
import com.runrunfast.face.util.ToolUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

/**
 * 设备Service
 *
 * @author YangNing
 */
@Service
@Transactional
public class DeviceService {
    private Logger logger = LoggerFactory.getLogger(DeviceService.class);

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    CameraRepository cameraRepository;
    @Autowired
    DeviceRelationRepository deviceRelationRepository;
    @Autowired
    YoutuService youtuService;
    @Autowired
    ColourService colourService;

    public JSONObject addCamera(Camera camera) {
        JSONObject params = new JSONObject();
        params.put("name", camera.getName());
        params.put("device_group_id", camera.getDevice_group_id());
        params.put("box_id", camera.getBox_id());
        params.put("pic_size", camera.getPic_size());
        params.put("pic_type", camera.getPic_type());
        params.put("transcode_size", camera.getTranscode_size());
        if (ToolUtil.isNotEmpty(camera.getType())) {
            params.put("type", camera.getType());
        }
        if (ToolUtil.isNotEmpty(camera.getRtsp_url())) {
            params.put("rtsp_url", camera.getRtsp_url());
        }
        // TODO: 2018/5/21 recognize_uniq_interval是必传参数，文档中没写
        if (ToolUtil.isNotEmpty(camera.getRecognize_uniq_interval())) {
            params.put("recognize_uniq_interval", camera.getRecognize_uniq_interval());
        }
        HttpEntity httpEntity = youtuService.initEntity(params, YoutuUrl.CAMERAS, HttpMethod.POST);
        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(YoutuUrl.HOST + YoutuUrl.CAMERAS, httpEntity, JSONObject.class);
        if (0 == responseEntity.getBody().getInteger("code")) {
            //持久化摄像头信息
            cameraRepository.save(camera);
            DeviceRelation deviceRelation = new DeviceRelation();
            deviceRelation.setDeviceId("" + responseEntity.getBody().getJSONObject("data").getInteger("camera_id"));
            deviceRelation.setThingId(camera.getId());
            deviceRelation.setType(0);
            //持久化设备关联关系
            deviceRelationRepository.save(deviceRelation);
            JSONObject reportResult = colourService.reportThing(deviceRelation.getThingId(), camera.getName(), camera.getLocation());
            logger.info("添加摄像头上报Thing----->: " + reportResult.toJSONString());
        }
        return responseEntity.getBody();
    }

    public JSONObject addYouBox(YouBox youBox) {
        JSONObject params = new JSONObject();
        params.put("name", youBox.getName());
        params.put("device_group_id", youBox.getDevice_group_id());
        params.put("hardware_key", youBox.getHardware_key());
        if (ToolUtil.isNotEmpty(youBox.getInfo())) {
            params.put("info", youBox.getInfo());
        }
        HttpEntity httpEntity = youtuService.initEntity(params, YoutuUrl.BOXES, HttpMethod.POST);
        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(YoutuUrl.HOST + YoutuUrl.BOXES, httpEntity, JSONObject.class);
        if (0 == responseEntity.getBody().getInteger("code")) {
            DeviceRelation deviceRelation = new DeviceRelation();
            deviceRelation.setDeviceId("" + responseEntity.getBody().getJSONObject("data").getInteger("camera_id"));
            deviceRelation.setType(1);
            deviceRelationRepository.save(deviceRelation);
            // TODO: 2018/5/21 上报物联网
        }
        return responseEntity.getBody();
    }
}
